<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-yellow sidebar-mini">
<section class="content-header">
    <h1>项目文件查询</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <div class="form-inline">
                        <form id="queryForm" class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">项目编号</label>
                                <label class="control-label" col-md-8 style="text-align: left;padding-left: 5px;">
                                    <input type="text" name="proCode" class="form-control" placeholder="请输入项目编号">
                                </label>
                            </div>
                            <div class="form-group">
                                <label class="control-label"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">项目名称</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <input type="text" name="proName" class="form-control" placeholder="请输入项目名称">
                                </label>
                            </div>
                            <button id="btn-search" type="button" class="btn btn-success" style="margin-top: 5px">
                                搜索
                            </button>
                        </form>
                    </div>
                </div>
                <div class="box-body">
                    <div class="row">
                        <table id="bootstrap-table-project"></table>
                    </div>
                    <div id="div-file" class="row" style="display: none">
                        <div class="form-inline">
                            <form id="queryFileForm" class="form-horizontal">
                                <div class="form-group">
                                    <label class="control-label"
                                           style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">文件类型</label>
                                    <label class="control-label" style="text-align:left;padding-left: 5px;">
                                        <select id="select_fileType" class="form-control selectpicker"
                                                data-actions-box="true"
                                                multiple>
                                            <option selected="selected" value="1">询价单附件</option>
                                            <option selected="selected" value="2">项目任务单</option>
                                            <option selected="selected" value="3">项目附件</option>
                                            <option selected="selected" value="4">技术协议</option>
                                        </select>
                                        <input name="fileTypes" value="1,2,3,4" type="hidden" style="display: none"/>
                                        <input name="proCode" type="hidden" style="display: none"/>
                                    </label>
                                </div>
                                <div class="form-group" shiro:hasPermission="pro:fileQuery:download"
                                     style="padding-top: 8px;margin-left: 0px;">
                                    <button type="button" class="btn btn-info" onclick="downloadPackage()">打包下载</button>
                                </div>
                            </form>
                            <form id="downloadPackageForm" enctype="application/x-www-form-urlencoded;charset=utf-8" action="/prodOrd/downloadPackage" style="display: none" method="get">
                                <input name="fileIds" type="hidden" value=""/>
                                <input name="fileTypes" type="hidden" value="" />
                            </form>
                        </div>
                    </div>
                    <div class="row">
                        <table id="bootstrap-table-project-file"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let projectTable, fileTable;
    $(function () {
        $("#select_fileType").change(function () {
            $("#queryFileForm [name='fileTypes']").val($("#select_fileType").val());
            info($("#queryFileForm [name='proCode']").val());
        });
        projectTable = js.table.init({
            id: 'bootstrap-table-project',
            url: ctx + "prodOrd/loadProjectByAttachmentList",
            showExport: false,
            pagination: true,
            pageSize: 4,
            paginationVAlign: 'bottom',
            onLoadSuccess:function (data){
                if(data.rows.length>0){
                    info(data.rows[0].proCode);
                }
            },
            columns: [
                {
                    title: '序号', width: '60',
                    formatter: function (value, row, index, field) {
                        var pageSize = projectTable.bootstrapTable('getOptions').pageSize;
                        var pageNumber = projectTable.bootstrapTable('getOptions').pageNumber;
                        return pageSize * (pageNumber - 1) + index + 1;
                    }
                },
                {
                    title: '项目编号', field: 'proCode', width: '120px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '项目名称', field: 'proName',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '数量', field: 'proQty', width: '90px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '需方', field: 'customer', width: '180px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '安装地点', field: "insSite",width: '180px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '询价单附件', field: "inquirySheetAttachmentNum",width: "110"
                },
                {
                    title: '项目任务单附件', field: "projectTaskListAttachmentNum",width: "110"
                },
                {
                    title: '项目附件', field: 'projectAttachmentNum',width: "110"
                },
                {
                    title: '技术协议', field: 'technicalAgreementAttachmentNum',width: "110"
                },
                {
                    title: '操作', width: '60px',
                    formatter: function (value, row, index) {
                        let elements = new Array();
                        if (hasP('pro:fileQuery:view') && (row.inquirySheetAttachmentNum > 0 || row.projectTaskListAttachmentNum > 0 || row.projectAttachmentNum > 0 || row.technicalAgreementAttachmentNum > 0)) {
                            elements.push('<button class="btn btn-info btn-xs" onclick="info(\'' + row.proCode + '\')">查看</button>');
                        }
                        return elements.join('');
                    }
                }
            ]
        });

        /**
         * 表格搜索
         */
        $("#btn-search").click(function () {
            js.table.search(projectTable);
        });
    });
    /**
     * @methodName: info
     * @description: 加载文件列表
     * @param: proCode
     * @return：
     * @exception：
     * @author： 徐一贺 tel:18941916590
     * @designer： 陈欣，盖世奇
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/3/27 8:07
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    function info(proCode) {
        $("#queryFileForm [name='proCode']").val(proCode);
        if($("#queryFileForm [name='proCode']").val()!=null){
            $('#bootstrap-table-project-file').bootstrapTable('destroy');
            fileTable = js.table.init({
                id: 'bootstrap-table-project-file',
                url: ctx + "prodOrd/loadProjectAttachmentList",
                queryForm: 'queryFileForm',
                showExport: false,
                pagination: true,
                pageSize: 8,
                paginationVAlign: 'bottom',
                onLoadSuccess: function (data) {
                    $("#div-file").show();
                },
                columns: [
                    {
                        checkbox: true,
                        width: '30px',
                        formatter: function (value, row, index) {
                            // 类型2无法打包下载
                            if (row.fileType != 2) {
                                return {disabled: false}
                            } else {
                                return {disabled: true}
                            }
                        }
                    },
                    {
                        title: '序号', width: '60',
                        formatter: function (value, row, index, field) {
                            var pageSize = projectTable.bootstrapTable('getOptions').pageSize;
                            var pageNumber = projectTable.bootstrapTable('getOptions').pageNumber;
                            return pageSize * (pageNumber - 1) + index + 1;
                        }
                    },
                    {
                        title: '文件类型', field: 'fileType', width: '120',
                        formatter: function (value, row, index) {
                            if (value != null) {
                                if (value === 1) {
                                    return '询价单附件';
                                } else if (value === 2) {
                                    return '项目任务单附件';
                                } else if (value === 3) {
                                    return '项目附件';
                                } else if (value === 4) {
                                    return '技术协议';
                                }
                            } else {
                                return '--';
                            }

                        }
                    },
                    {
                        title: '文件名称', field: 'fileName', width: '600',
                        formatter: function (value, row, index) {
                            if (value != null) {
                                return value;
                            } else {
                                return '--';
                            }

                        }
                    },
                    {
                        title: '备注', field: 'fileRemark',
                        formatter: function (value, row, index) {
                            if (value != null && value.trim() != '') {
                                return value;
                            } else {
                                return '--';
                            }
                        }
                    },
                    {
                        title: '上传时间', field: 'uploadTime', width: '90',
                        formatter: function (value, row, index) {
                            if (value != null) {
                                return value;
                            } else {
                                return '--';
                            }

                        }
                    },
                    {
                        title: '操作', width: '60px',
                        formatter: function (value, row, index) {
                            let elements = new Array();
                            if (hasP('pro:fileQuery:download')) {
                                if(row.fileType===2){
                                    elements.push('<a href="/prodOrd/viewProInvoice/'+row.fileId+'" target="_blank" class="btn btn-info btn-xs">查看</a>');
                                }else{
                                    elements.push('<a href="/prodOrd/downloadFile/'+row.fileId+'/'+row.fileType+'" class="btn btn-info btn-xs">下载</a>');
                                }
                            }
                            return elements.join('');
                        }
                    }
                ]
            });
        }
    }

    /**
     * 打包下载
     */
    function downloadPackage() {
        let fileIdArr = js.table.selectColumns(fileTable, "fileId");
        let fileTypeArr = js.table.selectColumns(fileTable, "fileType");
        if(fileIdArr.length>0){
            let fileIds="";
            let fileTypes="";
            for (let i = 0; i < fileIdArr.length; i++) {
                fileIds+=fileIdArr[i]+',';
                fileTypes+=fileTypeArr[i]+',';
            }
            fileIds=fileIds.substr(0,fileIds.length-1);
            fileTypes=fileTypes.substr(0,fileTypes.length-1);
            $("#downloadPackageForm [name='fileIds']").val(fileIds);
            $("#downloadPackageForm [name='fileTypes']").val(fileTypes);
            $("#downloadPackageForm").submit();
            $("#downloadPackageForm [name='fileIds']").val('');
            $("#downloadPackageForm [name='fileTypes']").val('');
        }else{
            js.modal.warning("请选择要打包下载的文件！");
        }
    }
</script>
</body>
</html>